<template>
  <div class="transition-box">
    <div class="content">
      <div class="header">
        <div class="title">{{ songInfo.name }}</div>
        <div class="author">{{ songInfo.ar[0].name }}</div>
      </div>
      <div class="line">
        <img src="../assets/needle.png" alt="" />
      </div>
      <div class="cd">
        <img id="cd" src="../assets/disc.png" alt="" />
        <div class="pic">
          <img :src="songInfo.al.picUrl" :style="setAnimation()"/>
        </div>
      </div>
      <div class="lyrics">
        <ul>
          <li v-for="(item,i) in lyricsObjArr" :key="item.uid" :data-index='i'>
            {{item.lyric}}
          </li>
        </ul>
      </div>
    </div>
    <div class="comments">
      <div class="title">部分评论({{comments3.length}})</div>
      <ul>
        <li v-for="(item, index) in comments3" :key="index">
          <div class="avatar" @click="toUserProfile(item.user.userId)">
            <img :src="item.user.avatarUrl" alt="">
          </div>
          <div class="comment">{{item.content}}<div class="time">{{item.timeStr}}</div>
          </div>
        </li>
        
      </ul>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { mapState } from "vuex";
export default {
  name: "Singing",
  props: ["changeIsShowSinging"],
  data() {
    return {
      lyric: "",
      lyricsObjArr: [],
      comments3: [],
      isAnimation:false
    };
  },
  computed: {
    ...mapState("songAbout", ["songInfo", "playing"]),
  },
  methods: {
    //关闭全屏播放歌曲页面
    changeIsShow() {
      this.changeIsShowSinging();
    },

    formatLyricTime(time) {
      // 格式化歌词的时间 转换成 sss:ms
      const regMin = /.*:/;
      const regSec = /:.*\./;
      const regMs = /\./;

      const min = parseInt(time.match(regMin)[0].slice(0, 2));
      let sec = parseInt(time.match(regSec)[0].slice(1, 3));
      const ms = time.slice(
        time.match(regMs).index + 1,
        time.match(regMs).index + 3
      );
      if (min !== 0) {
        sec += min * 60;
      }
      return Number(sec + "." + ms);
    },

    chuligeci() {
      this.lyricsObjArr = []
      const regNewLine = /\n/;
      this.lyric = this.lyric.split(regNewLine);
      // console.log(this.lyric);

      const regTime = /\[\d{2}:\d{2}.\d{2,3}\]/;

      this.lyric.forEach((item) => {
        if (item === "") return;
        const obj = {};
        const time = item.match(regTime);

        obj.lyric =
          item.split("]")[1].trim() === "" ? "" : item.split("]")[1].trim();
        obj.time = time
          ? this.formatLyricTime(time[0].slice(1, time[0].length - 1))
          : 0;
        obj.uid = Math.random().toString().slice(-6);
        if(obj.lyric !== " "){
          this.lyricsObjArr.push(obj)
        }
      });

      // this.$bus.emit("geci")
    },

    setAnimation(){
      var style = {};
      if(this.isAnimation){
        style.animation = "rotate 10s infinite linear forwards";
      }
      return style;
    },

    toUserProfile(id){
      this.changeIsShow()
      this.$bus.$emit("routeToUserProfile",id);
    }

    //歌曲封面旋转
  },
  watch: {
    songInfo: {
      immediate: true, //初始化时让handler调用一下
      //handler什么时候调用？当isHot发生改变时。
      handler(newValue) {
        console.log(newValue.id);
        axios
          .get("http://localhost:8013/api/lyric?id=" + newValue.id)
          .then((res) => {
            this.lyric = res.data.lrc.lyric;
            this.chuligeci();
          });
        axios.get("http://localhost:8013/api/comment/new?type=0&id="+newValue.id+"&sortType=1").then(
          res=>{
            this.comments3 = res.data.data.comments
            console.log(res.data.data.comments);
          }
        )
      },
    },
    playing(){
      if(this.playing){
        //cd旋转
        this.isAnimation = true
      }else{
        //cd停止旋转
        this.isAnimation = false
      }
    }
  },
  
  mounted() {
    // console.log(this.songInfo.id);
  },
};
</script>

<style scoped>
.transition-box {
  padding-bottom: 80px;
  text-align: left;
  position: absolute;
  z-index: 13;
  top: 0;
  left: 0;
  width: 100%;
  height: auto;
  background: linear-gradient(rgb(255, 235, 235), rgb(245, 245, 245));
}

.content {
  width: 1000px;
  margin: auto;
}
.content .header {
  margin: auto;
  height: 88px;
  text-align: center;
  padding-top: 80px;
  color: #888;
  margin-bottom: 20px;
}
.header .title {
  font-size: 24px;
  font-weight: 500;
  padding-bottom: 20px;
  color: #333;
}
.line {
  position: absolute;
  left: 370px;
  width: 200px;
  height: 30px;
  z-index: 99;
}
.cd {
  z-index: 9;
  width: 300px;
  height: 300px;
  background: rgb(194, 167, 167);
  position: relative;
  border-radius: 50%;
  margin-top: 80px;
}
#cd {
  width: 92%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
img {
  width: 100%;
}
.line img {
  width: 70%;
  transform-origin: left top;
  /* transform: rotateZ(-20deg); */
  /* width: 30%;
  z-index: 99;
  transform-origin: left top; */
  /* animation: play 0.5s linear; */
}
@keyframes play {
  0% {
    transform: rotateZ(-20deg);
  }
  100% {
    transform: rotateZ(5deg);
  }
}
@keyframes pause {
  0% {
    transform: rotateZ(5deg);
  }
  100% {
    transform: rotateZ(-20deg);
  }
}

@keyframes rotate {
  0% {
    transform: rotateZ(0deg);
  }
  100% {
    transform: rotateZ(360deg);
  }
}
.pic {
  width: 189px;
  height: 189px;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.pic img {
  width: 100%;
  border-radius: 50%;
}
.lyrics {
  text-align: center;
  position: absolute;
  left: 686px;
  top: 230px;
  width: 440px;
  height: 350px;
  overflow-y: scroll;
}
.lyrics ul li {
  height: 40px;
  line-height: 40px;
  font-size: 15px;
  color: #888;
}
.similar {
  width: 240px;
  height: 300px;
  border: 1px solid #fff;
  position: absolute;
  right: 200px;
  top: 180px;
}
.comments {
  width: 800px;
  margin: auto;
  padding: 20px 0;
}
.comments .title {
  font-weight: 600;
  text-align: left;
  margin-bottom: 10px;
}
.comments li {
  width: 100%;
  position: relative;
  padding: 15px 0px 15px 45px;
  font-size: 13px;
  color: #333;
  border-bottom: 1px solid #ddd;
}
.comments li .avatar {
  position: absolute;
  left: 0px;
  top: 20px;
  width: 35px;
  height: 35px;
  border-radius: 50%;
  overflow: hidden;
}
.avatar img {
  width: 100%;
}
ul {
  list-style-type: none;
  line-height: 20px;
}
.comments li .comment {
  left: 45px;
  text-align: left;
  font-size: 13px;
}
.comments li .time {
  color: #888;
  font-size: 12px;
  margin-top: 10px;
}
.like {
  position: absolute;
  right: 0px;
  bottom: 10px;
}
.like span {
  margin: 8px;
  text-align: center;
}
.like span:nth-child(3) {
  border-right: none;
}
</style>